package qlsl.androiddesign.view.subview.activityview.peoplecenterbase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.baseactivity.BaseActivity;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 公用ListView页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public abstract class PeopleCenterBaseView<T, B extends BaseActivity> extends FunctionView<B> {

	protected PullToRefreshListView refreshView;

	protected List<T> list = new ArrayList<T>();

	protected Pager pager;

	public PeopleCenterBaseView(B activity) {
		super(activity);
		setContentView(R.layout.common_activity_refresh_list_view);
	}

	protected void initView(View view) {
		refreshView = findViewById(R.id.refreshListView);
	}

	protected void initData() {

	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	@SuppressWarnings("unchecked")
	public <A> void showData(A... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<T> list_result = (List<T>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		}
	}

	@SuppressWarnings("hiding")
	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {

	}

	protected void resetList(List<T> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	protected void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = getAdapter();
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	public abstract BaseAdapter getAdapter();

	private OnRefreshListener<ListView> refreshListener = new OnRefreshListener<ListView>() {
		@SuppressWarnings("deprecation")
		public void onRefresh(PullToRefreshBase<ListView> refreshView) {
			refreshView.setRefreshingLabel(
					SoftwareApplication.getInstance().getResources().getString(R.string.refreshing_label));
			refreshView.onLoadComplete();
		}

	};

}
